
import {httpGet, httpPost, httpPut} from '@/api';
import {checkUnique} from '@/api/system/commonFunc';

export const EditMixin = {
    data() {
      return {
        // 遮罩层
        loading: false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //操作类型，add添加，update修改
        operType:"",
        //form表单组件名称
		    formRef: "form",
        //表单禁用
        formDisabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    methods: {
      reset() {
        this.form = {};
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$refs[this.formRef].clearValidate()
      },

      /** 打开添加界面 */
      toAdd(title, proms, callback) {
        this.openDialog(title, "add", proms, callback);
      },

      /** 打开修改界面 */
      toUpdate(title, proms, callback) {
        this.openDialog(title, "update", proms, callback);
      },

      /** 打开详情界面 */
      toDetail(title, proms, callback) {
        this.openDialog(title, "detail", proms, callback);
      },

      /** 打开弹窗 */
      openDialog(title, operType, proms, callback) {
        proms = proms instanceof Promise ? proms : new Promise(resolve=>resolve({data:proms}));
        proms.then(res => {
          if(this.beforeOpen(title, operType, res)){
            return ;
          }
          this.openD({
            form: res.data,
            operType: operType,
            title: title
          }, callback);
        });
        //此方法可以在具体的组件里面进行覆盖，可以先调用this.openD(op)方法之后，然后再写自己的具体逻辑
      },

      /** 在执行proms后打开弹窗之前，返回true则中断打开弹窗 */
      beforeOpen(title, operType, res){
      },

      /** 打开弹窗 */
      openD(op, callback) {
        this.reset();
        op = Object.assign({title:"", operType:"detail"}, op);
        this.open = true;
        this.operType = op.operType;
        this.title = op.title;
        this.formDisabled = op.operType == 'detail';
        Object.assign(this.form, op.form);
        if(callback && callback instanceof Function){
          callback(this.form)
        }
      },

      checkUniqueAsyn(value, callback, tableUniqueCode, param){
        if(!value&&value!==0){
          callback()
          return;
        }
        checkUnique(tableUniqueCode, this.operType, param).then(res=>{
          callback(res.data === 'true'? undefined : new Error(value+' 已存在'));
        }).catch(res=>{})
      },

      saveForm(param0, data) {
        this.$refs[this.formRef].validate(valid => {
          if (valid) {
            data = data ? data : this.form;
            this.loading = true;
            let proms;
            if(param0 instanceof Function){
              proms = param0(data);
            }else{
              const method = this.operType=='update' ? httpPut : httpPost;
              proms = method(param0, data);
            }
            proms.then(res => {
              if (res && res.success) {
                this.msgSuccess("操作成功");
                this.open = false;
                this.$emit('ok', res.data?res.data:Object.assign({}, data), this.operType);
              }
            }).catch(()=>{}).finally(()=>{
              this.loading = false;
            });
          }
        });
      },
    }
  };
